package com.example.demo.sign;

import com.alibaba.fastjson2.JSON;
import io.netty.util.internal.StringUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @program: demo
 * @description:
 * @author: zt22328 赵儒定
 * @create: 2022-06-08 09:22
 **/
public class Sign {



    public static void main(String[] args) {
        Map<String, Object> stringObjectMap = new HashMap<>();
        stringObjectMap.put("a","123");
        stringObjectMap.put("r","rew");
        stringObjectMap.put("c","rew");
        stringObjectMap.put("d", Arrays.asList("12","2","3"));

        Map<String, Object> stringObjectMap3 = new HashMap<>();
        stringObjectMap3.put("a","123");
        stringObjectMap3.put("r","rew");
        stringObjectMap3.put("c","rew");
        stringObjectMap3.put("d", Arrays.asList("12","2","3"));

        Map<String, Object> stringObjectMap2 = new HashMap<>();
        stringObjectMap2.put("a","123");
        stringObjectMap2.put("r","rew");
        stringObjectMap2.put("c","rew");
        stringObjectMap2.put("d", Arrays.asList(stringObjectMap3,stringObjectMap3,stringObjectMap3));
        stringObjectMap.put("mp2",stringObjectMap2);


        System.out.println(stringObjectMap);
        TreeMap treeMap = JSON.to(TreeMap.class, JSON.toJSONString(stringObjectMap));

//        TreeMap treeMap = new TreeMap(stringObjectMap);
        System.out.println(getSign(JSON.toJSONString(stringObjectMap)));
    }

    public static String getSign(String json){
        if(StringUtil.isNullOrEmpty(json)){
            return null;
        }
        StringBuilder sign = new StringBuilder();
        TreeMap<String,Object> treeMap = JSON.to(TreeMap.class, json);
        treeMap.forEach((k,v)->{
            signMap(sign,k,v);
        });

        return sign.toString().substring(1);
    }

    public static String getSignByMap(Map<String,Object> map){
        if(CollectionUtils.isEmpty(map)){
            return null;
        }
        StringBuilder sign = new StringBuilder();
        TreeMap<String,Object> treeMap = new TreeMap(map);
        treeMap.forEach((k,v)->{
            signMap(sign,k,v);
        });

        return sign.toString().substring(1);
    }

    private static void signMap(StringBuilder sign, String k, Object v) {
        if(v==null){
            return;
        }
        if(v instanceof Map){
            sign.append("&").append(k).append("={").append(getSignByMap((Map)v)).append("}");
        }else if(v instanceof Collection){
            sign.append("&").append(k).append("=[").append(getSignByCollection((Collection)v)).append("]");
        }else {
            sign.append("&").append(k).append("=").append(v);
        }
    }

    private static String getSignByCollection(Collection<Object> collection) {
        StringBuilder sign = new StringBuilder();
        collection.forEach(v->{
            if(v==null){
                return;
            }
            if(v instanceof Map){
                sign.append(",{").append(getSignByMap((Map)v)).append("}");
            }else if(v instanceof Collection){
                sign.append(",[").append(getSignByCollection((Collection)v)).append("]");
            }else {
                sign.append(",").append(v);
            }
        });
        return sign.toString().substring(1);
    }

}
